<template>
  <el-dialog
    v-el-drag-dialog
    title="关联设备"
    :visible.sync="dialogVisible"
    width="60%"
  >
    <el-form ref="mainForm" size="medium" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="气象站" prop="weather_sn">
        <el-select v-model="formData.weather_sn" clearable filterable class="full-text">
          <el-option
            v-for="(item,index) in qxzs"
            :key="index"
            :label="item.name"
            :value="item.serialno"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="墒情站" prop="mois_sn">
        <el-select v-model="formData.mois_sn" clearable filterable multiple class="full-text">
          <el-option
            v-for="(item,index) in sqzs"
            :key="index"
            :label="item.name"
            :value="item.serialno"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit('mainForm')">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { dclassCodeMap } from '@/utils/constant'
export default {
  props: {
    field: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      formData: {
        weather_sn: '',
        mois_sn: []
      },
      dialogVisible: false,
      rules: {
        weather_sn: [
          { required: true, message: '请选择关联气象站', trigger: 'blur' }
        ],
        mois_sn: [
          { required: true, message: '请选择关联墒情站', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters(['devices']),
    qxzs() {
      return this.devices.filter(val => {
        return +val.product_type === dclassCodeMap.qxz
      })
    },
    sqzs() {
      return this.devices.filter(val => {
        return +val.product_type === dclassCodeMap.sqz
      })
    }
  },
  watch: {
    field: {
      handler(newVal) {
        this.formData.weather_sn = newVal.weather_sn
        this.formData.mois_sn = newVal.mois_sn
      },
      deep: true,
      immediate: true
    }
  },
  methods: {
    show() {
      this.dialogVisible = true
    },
    hide() {
      this.dialogVisible = false
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit('deviceSubmit', this.formData)
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
